package com.rpframework.website.daole.act.admin;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.rpframework.core.api.FileService;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.daole.domain.User;
import com.rpframework.website.daole.service.UserService;

/**
 * 用户表
 * @author zl
 * @date 2015年12月14日 下午4:04:01
 */
@Controller("yh_user")
@RequestMapping("/admin/user")
public class AdminUserAct extends AdminAct{
	@Resource UserService service;
	@Resource FileService fileService;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/list")
	public String list(@RequestParam(value="pager", required=false) Pager pager, 
			@RequestParam(value="state", required=false) Integer state, 
			Map<Object, Object> model){
		if(pager == null) {
			pager = new Pager();
		}
		if(NumberUtils.isValid(state)){
			pager.getSearchMap().put("state", state.toString());
			model.put("state", state);
		}
		pager = service.getPager(pager);
		model.put("pager", pager);
		return this.doPackageURI("user/list");
	}
	
	@RequestMapping("/{userId}/detail")
	public String detail(@PathVariable Integer userId,RedirectAttributes attr, Map<Object, Object> model,HttpSession session){
		User user = service.select(userId);
		Assert.notNull(user,"Not find userId: " + userId);
		model.put("user", user);
		
		return this.doPackageURI("user/detail");
	}
	
	@RequestMapping("/on")
	public @ResponseBody JsonElement onState(@RequestParam(value="id", required=false) Integer id){
		JsonObject json = new JsonObject();
		boolean flag = service.doUpdateState(id);
		if(flag){
			json.addProperty("succ", true);
		}else{
			json.addProperty("succ", false);
		}
		return json;
	}
	@RequestMapping("/off")
	public @ResponseBody JsonElement offState(@RequestParam(value="id", required=false) Integer id){
		JsonObject json = new JsonObject();
		boolean flag = service.doUpdateState(id);
		if(flag){
			json.addProperty("succ", true);
		}else{
			json.addProperty("succ", false);
		}
		return json;
	}
}
